import { LandFormData, AddLandForm, EditLandForm, LandInfoItem } from '../types/land';

/**
 * 表单数据转换工具类
 * 用于在前端表单数据和API数据之间进行转换
 */
export class LandFormConverter {
    /**
     * 将表单数据转换为新增土地的API格式
     */
    static toAddForm(formData: LandFormData): AddLandForm {
        return {
            id: Number(formData.id),
            landName: String(formData.landName),
            landsize: String(formData.landsize),
            landQuality: String(formData.landQuality),
            climate: String(formData.climate),
            previousCro: String(formData.previousCro),
            isActive: Number(formData.isActive),
            landImage: String(formData.landImage),
            landAddress: String(formData.landAddress),
            isPlanting: Boolean(formData.isPlanting),
            longitude: Number(formData.longitude),
            latitude: Number(formData.latitude),
            provinceCode: String(formData.provinceCode),
            cityCode: String(formData.cityCode),
            areaCode: String(formData.areaCode)
        };
    }

    /**
     * 将表单数据转换为编辑土地的API格式
     */
    static toEditForm(formData: LandFormData): EditLandForm {
        return {
            id: Number(formData.id),
            roleId: Number(formData.roleId),
            landName: String(formData.landName),
            landsize: String(formData.landsize),
            landQuality: String(formData.landQuality),
            climate: String(formData.climate),
            previousCro: String(formData.previousCro),
            isActive: Number(formData.isActive),
            landImage: String(formData.landImage),
            landAddress: String(formData.landAddress),
            isPlanting: Boolean(formData.isPlanting),
            longitude: Number(formData.longitude),
            latitude: Number(formData.latitude),
            provinceCode: String(formData.provinceCode),
            cityCode: String(formData.cityCode),
            areaCode: String(formData.areaCode)
        };
    }

    /**
     * 将土地信息项转换为表单数据格式
     */
    static fromLandItem(item: LandInfoItem): LandFormData {
        return {
            id: item.id || 0,
            roleId: item.roleId || 1,
            landName: item.landName || '',
            landsize: item.landsize || 0,
            landQuality: item.landQuality || '',
            climate: item.climate || '',
            previousCro: item.previousCro || '',
            isActive: item.isActive || 0,
            landImage: item.landImage || '',
            landAddress: item.landAddress || '',
            isPlanting: item.isPlanting || false,
            createTime: item.createTime || '',
            longitude: item.longitude || 0,
            latitude: item.latitude || 0,
            provinceCode: item.provinceCode || '',
            cityCode: item.cityCode || '',
            areaCode: item.areaCode || '',
            // updateTime: item.updateTime || ''
        };
    }

    /**
     * 创建空的表单数据
     */
    static createEmptyForm(): LandFormData {
        return {
            id: 0,
            // roleId: 1, // 设置默认roleId
            landName: '',
            landsize: 0,
            landQuality: '好',
            climate: '',
            previousCro: '',
            isActive: 0,
            landImage: '',
            landAddress: '',
            isPlanting: false,
            createTime: '',
            longitude: 0,
            latitude: 0,
            provinceCode: '',
            cityCode: '',
            areaCode: '',
            updateTime: ''
        };
    }

    /**
     * 验证表单数据是否有效
     */
    static validateForm(formData: LandFormData): { valid: boolean; errors: string[] } {
        const errors: string[] = [];
        if (!formData.landName) errors.push('土地名称不能为空');
        if (!formData.landsize || isNaN(Number(formData.landsize)) || Number(formData.landsize) <= 0) errors.push('土地面积必须为大于0的数字');
        if (!formData.climate) errors.push('气候类型不能为空');
        if (!formData.landAddress) errors.push('土地地址不能为空');
        if (!formData.provinceCode) errors.push('请选择省份');
        if (!formData.cityCode) errors.push('请选择城市');
        if (!formData.areaCode) errors.push('请选择区县');
        return {
            valid: errors.length === 0,
            errors
        };
    }
} 